﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;
using System.Globalization;
namespace coffeeshop.Forms

{
    public partial class frmWareHouseHistorical : Form
    {
        public frmWareHouseHistorical()
        {
            InitializeComponent();
            this.lstHistory.FullRowSelect = true;
            this.lstHistory.GridLines = true;
            this.lstHistory.View = System.Windows.Forms.View.Details;
            lstHistory.Columns.Add("Ngày", 140);
            lstHistory.Columns.Add("Mặt hàng", 240);
            this.lstHitoryDetail.FullRowSelect = true;
            this.lstHitoryDetail.GridLines = true;
            this.lstHitoryDetail.View = System.Windows.Forms.View.Details;
            lstHitoryDetail.Columns.Add("Mặt hàng", 140);
            lstHitoryDetail.Columns.Add("Mã hàng", 80);
            lstHitoryDetail.Columns.Add("Số lượng", 80);
            lstHitoryDetail.Columns.Add("Đơn vị", 80);
            loadData();
            this.lstHistory.Focus();
            if (lstHistory.Items.Count > 0)
                this.lstHistory.Items[0].Selected = true;
        }

        public void loadData()
        {
            List<ProductInventory> Data = ProductInventory.getWarehouseHistorical();
            for (int i = 0; i < Data.Count; i++)
            {
                DateTime s = Data[i].CreatedDate;
                string _Name = Data[i].Name;
                for (int j = i + 1; j < Data.Count; j++)
                {
                    if (Data[i].CreatedDate.AddDays(-1) < Data[j].CreatedDate &&
                        Data[j].CreatedDate < Data[i].CreatedDate.AddDays(1) &&
                        Data[i].CreatedDate.Day == Data[j].CreatedDate.Day)
                    {
                       _Name = _Name +", " + Data[j].Name;
                       Data.RemoveAt(j);
                       j= j - 1;
                    }    
                }
                ListViewItem item = new ListViewItem();
                item.Text = s.ToString("dd/MM/yyyy");
                item.SubItems.Add(_Name);
                lstHistory.Items.Add(item);
            }
        }


        private void lstHistory_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstHitoryDetail.Items.Clear();
            if (lstHistory.SelectedIndices.Count>0)
            {
                string date = lstHistory.SelectedItems[0].Text;
                DateTime _dtCreatedDate = DateTime.Parse(date, new CultureInfo("en-GB", false));
                lblCreatedDate.Text = "Chi tiết nhập hàng ngày " + _dtCreatedDate.ToString("dd/MM/yyyy");
                DataTable Data = ProductInventory.getWarehouseHisDetail(_dtCreatedDate);
                foreach (DataRow row in Data.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = (row["Name"].ToString());
                    item.SubItems.Add(row["Code"].ToString());
                    item.SubItems.Add(row["ReceiptQuantity"].ToString());
                    item.SubItems.Add(row["UnitName"].ToString());
                    lstHitoryDetail.Items.Add(item);
                }
            }
        }

    }
}
